package com.sixth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sixth.dao.SubjectDao;
import com.sixth.entity.Subject;
import com.sixth.service.SubjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: self-tui-education
 * @description:
 * @author: 高振杨
 * @create: 2022-05-08 20:07
 **/
@Service
public class SubjectServiceImpl implements SubjectService {
    @Autowired
    private SubjectDao subjectDao;
    @Override
    public List<Subject> findAllSubject() {
        QueryWrapper<Subject> wrapper = new QueryWrapper<>();
      /*  wrapper.eq("status_id","1");
        wrapper.eq("parent_id","0");*/
        List<Subject> subjects = subjectDao.selectList(wrapper);
        List<Subject> list = new ArrayList<>();
        for (Subject s:subjects){
            if (s.getParentId().equals("0")) {
                list.add(s);
            }
        }
        //方法的递归调用
        for (Subject one:list){
            List<Subject> children = findChildren(subjects, one.getId());
            one.setChildren(children);
        }

        return list;
    }

    List<Subject> findChildren(List<Subject> subjectVos,String fatherid){
        List<Subject> children = new ArrayList<>();
        for (Subject Subject:subjectVos){
            if (Subject.getParentId().equals(fatherid)){
                children.add(Subject);
            }
        }
        for (Subject c:children){
            List<Subject> children1 = findChildren(subjectVos, c.getId());
            c.setChildren(children1);
        }
        return children;
    }
}
